﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using Yoozoo.Gta.Common;
using Yoozoo.UI.YGUI;

public class UIPointerEffect : MonoBehaviour, IPointerClickHandler
{
    public GameObject effectPrefab; // 特效预制体
    public ParticleSystem[] particles;

    private RectTransform rectTransform; // 按钮的RectTransform组件
    private RectTransform target;
    private Camera camera;

    private void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        target = effectPrefab.transform.GetComponent<RectTransform>();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left)
            return;

        Vector2 fxLocalPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            rectTransform, eventData.position, ResidentHandler.Inst.UICamera, out fxLocalPos);
        
        //修正特效位置
        target.anchoredPosition = fxLocalPos;

        //播放点击效果
        foreach (ParticleSystem ps in particles)
        {
            ps.SetActive(true);
            ps.Play();
        }
    }
}
